<template>
    <el-dialog title="批量导入" :visible.sync="uploadshow" width="50% !important" :show-close="false" :close-on-click-modal="false">
        <div style="display: flex; justify-content: space-between;">
            <div></div>
            <!-- <el-link type="primary" @click="getUpdata">获取导入模板</el-link> -->
            <el-link type="primary"
                @click="downloadImg('http://203.57.5.167:8000/equipment/downloadFileServlet', '导入模板.xlsx')">获取导入模板</el-link>
        </div>
        <div>
            <el-upload action="#" list-type="text" drag multiple :auto-upload="false" :on-change="upchangeexl"
                accept=".xlsx, .xls" class="input_card upload-demo">
                <div style="height: 130px;">
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传Excel文档，且不超过500kb</div>
                </div>
            </el-upload>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="cancelWorkOrder">取 消</el-button>
            <el-button type="primary" @click="determineWorkOrder">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { getData, downLoad } from '@/api/table'
import { Message } from 'element-ui'
export default {
    props: {
        uploadshow: {
            type: Boolean,
            required: true,
        },
    },
    data() {
        return {
            uploadE: {},
            url: 'equipment/upload',
            formdata: null,
            // data: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            url: '',
            defaultProps: {
                children: 'children',
                label: 'label'
            },
        };
    },
    computed: {

    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    mounted() {

    },
    methods: {

        upchangeexl(data) {
            this.uploadE = data.raw
        },
        //获取导入模板
        downloadImg(url, fileName) {
            const x = new window.XMLHttpRequest();
            x.open('GET', url, true);
            x.responseType = 'blob';
            x.onload = () => {
                const url = window.URL.createObjectURL(x.response);
                const a = document.createElement('a');
                a.href = url;
                a.download = fileName;
                a.click();
            };
            x.send();
        },
        // getUpdata() {
        //     const url = 'http://203.57.5.167:8000/equipment/downloadFileServlet'
        //     downLoad(url, 'GET').then(res => {
        //         console.log(res);
        //         this.downloadhttp = res.msg
        //         if (res) {
        //             const blob = new Blob([res.msg], {
        //                 type: "application/vnd.ms-excel",
        //             });
        //             if (window.navigator.msSaveOrOpenBlob) {
        //                 navigator.msSaveBlob(
        //                     blob,
        //                     "设备模板.xls"
        //                 );
        //             } else {
        //                 var link = document.createElement("a");
        //                 link.href = window.URL.createObjectURL(blob);
        //                 link.download = "设备模板.xls";
        //                 link.click();
        //                 //释放内存
        //                 window.URL.revokeObjectURL(link.href);
        //             }
        //         }
        //     })
        // },
        cancelWorkOrder() {
            this.$emit('upload-event', false);
        },
        determineWorkOrder() {
            this.formdata = new FormData();
            this.formdata.append('uploadFile', this.uploadE)
            const url = '/equipment/upload'
            const met = 'post'
            getData(url, met, this.formdata).then(res => {
                Message({
                    message: res.msg,
                    type: 'success',
                    duration: 3 * 1000
                })
            })
            this.$emit('upload-event', false);
        }
    },
    created() {
    }
};
</script>

<style  scoped></style>